﻿using System.IO;
using System.Windows.Forms;
using System;

using System;
using System.IO;
using System.Windows.Forms;

namespace FileMerger
{
    public partial class MainForm : Form
    {
        private string file1Path = string.Empty;
        private string file2Path = string.Empty;

        public MainForm()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.btnSelectFile1 = new System.Windows.Forms.Button();
            this.btnSelectFile2 = new System.Windows.Forms.Button();
            this.btnMergeFiles = new System.Windows.Forms.Button();
            this.lblFile1 = new System.Windows.Forms.Label();
            this.lblFile2 = new System.Windows.Forms.Label();
            this.txtFile1 = new System.Windows.Forms.TextBox();
            this.txtFile2 = new System.Windows.Forms.TextBox();
            this.lblStatus = new System.Windows.Forms.Label();
            this.SuspendLayout();

            // btnSelectFile1
            this.btnSelectFile1.Location = new System.Drawing.Point(12, 12);
            this.btnSelectFile1.Name = "btnSelectFile1";
            this.btnSelectFile1.Size = new System.Drawing.Size(110, 23);
            this.btnSelectFile1.TabIndex = 0;
            this.btnSelectFile1.Text = "选择第一个文件";
            this.btnSelectFile1.UseVisualStyleBackColor = true;
            this.btnSelectFile1.Click += new System.EventHandler(this.btnSelectFile1_Click);

            // btnSelectFile2
            this.btnSelectFile2.Location = new System.Drawing.Point(12, 41);
            this.btnSelectFile2.Name = "btnSelectFile2";
            this.btnSelectFile2.Size = new System.Drawing.Size(110, 23);
            this.btnSelectFile2.TabIndex = 1;
            this.btnSelectFile2.Text = "选择第二个文件";
            this.btnSelectFile2.UseVisualStyleBackColor = true;
            this.btnSelectFile2.Click += new System.EventHandler(this.btnSelectFile2_Click);

            // btnMergeFiles
            this.btnMergeFiles.Location = new System.Drawing.Point(12, 70);
            this.btnMergeFiles.Name = "btnMergeFiles";
            this.btnMergeFiles.Size = new System.Drawing.Size(110, 23);
            this.btnMergeFiles.TabIndex = 2;
            this.btnMergeFiles.Text = "合并文件";
            this.btnMergeFiles.UseVisualStyleBackColor = true;
            this.btnMergeFiles.Click += new System.EventHandler(this.btnMergeFiles_Click);

            // lblFile1
            this.lblFile1.AutoSize = true;
            this.lblFile1.Location = new System.Drawing.Point(128, 17);
            this.lblFile1.Name = "lblFile1";
            this.lblFile1.Size = new System.Drawing.Size(65, 12);
            this.lblFile1.TabIndex = 3;
            this.lblFile1.Text = "第一个文件:";

            // lblFile2
            this.lblFile2.AutoSize = true;
            this.lblFile2.Location = new System.Drawing.Point(128, 46);
            this.lblFile2.Name = "lblFile2";
            this.lblFile2.Size = new System.Drawing.Size(65, 12);
            this.lblFile2.TabIndex = 4;
            this.lblFile2.Text = "第二个文件:";

            // txtFile1
            this.txtFile1.Location = new System.Drawing.Point(202, 14);
            this.txtFile1.Name = "txtFile1";
            this.txtFile1.ReadOnly = true;
            this.txtFile1.Size = new System.Drawing.Size(300, 21);
            this.txtFile1.TabIndex = 5;

            // txtFile2
            this.txtFile2.Location = new System.Drawing.Point(202, 43);
            this.txtFile2.Name = "txtFile2";
            this.txtFile2.ReadOnly = true;
            this.txtFile2.Size = new System.Drawing.Size(300, 21);
            this.txtFile2.TabIndex = 6;

            // lblStatus
            this.lblStatus.AutoSize = true;
            this.lblStatus.Location = new System.Drawing.Point(12, 105);
            this.lblStatus.Name = "lblStatus";
            this.lblStatus.Size = new System.Drawing.Size(41, 12);
            this.lblStatus.TabIndex = 7;
            this.lblStatus.Text = "状态: 就绪";

            // MainForm
            this.ClientSize = new System.Drawing.Size(514, 126);
            this.Controls.Add(this.lblStatus);
            this.Controls.Add(this.txtFile2);
            this.Controls.Add(this.txtFile1);
            this.Controls.Add(this.lblFile2);
            this.Controls.Add(this.lblFile1);
            this.Controls.Add(this.btnMergeFiles);
            this.Controls.Add(this.btnSelectFile2);
            this.Controls.Add(this.btnSelectFile1);
            this.Name = "MainForm";
            this.Text = "文件合并工具";
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private Button btnSelectFile1;
        private Button btnSelectFile2;
        private Button btnMergeFiles;
        private Label lblFile1;
        private Label lblFile2;
        private TextBox txtFile1;
        private TextBox txtFile2;
        private Label lblStatus;

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

        private void btnSelectFile1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "所有文件|*.*";
                openFileDialog.Title = "选择第一个文件";

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    file1Path = openFileDialog.FileName;
                    txtFile1.Text = file1Path;
                    UpdateStatus("已选择第一个文件");
                }
            }
        }

        private void btnSelectFile2_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "所有文件|*.*";
                openFileDialog.Title = "选择第二个文件";

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    file2Path = openFileDialog.FileName;
                    txtFile2.Text = file2Path;
                    UpdateStatus("已选择第二个文件");
                }
            }
        }

        private void btnMergeFiles_Click(object sender, EventArgs e)
        {
            try
            {
                // 检查是否已选择两个文件
                if (string.IsNullOrEmpty(file1Path) || string.IsNullOrEmpty(file2Path))
                {
                    MessageBox.Show("请先选择两个要合并的文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                // 创建Data目录（如果不存在）
                string dataDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
                if (!Directory.Exists(dataDirectory))
                {
                    Directory.CreateDirectory(dataDirectory);
                }

                // 生成新文件名（使用时间戳确保唯一性）
                string newFileName = $"MergedFile_{DateTime.Now:yyyyMMdd_HHmmss}.txt";
                string newFilePath = Path.Combine(dataDirectory, newFileName);

                // 读取第一个文件内容
                string content1 = File.ReadAllText(file1Path);

                // 读取第二个文件内容
                string content2 = File.ReadAllText(file2Path);

                // 合并内容并写入新文件
                string mergedContent = content1 + Environment.NewLine + content2;
                File.WriteAllText(newFilePath, mergedContent);

                UpdateStatus($"文件合并成功！新文件路径: {newFilePath}");
                MessageBox.Show($"文件合并成功！\n新文件路径: {newFilePath}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                UpdateStatus($"合并文件时出错: {ex.Message}");
                MessageBox.Show($"合并文件时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void UpdateStatus(string message)
        {
            lblStatus.Text = $"状态: {message}";
        }
    }
}